//
//  ECOrderDateSegePicker.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/11/14.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECOrderDateSegePicker.h"

@interface ECOrderDateSegePicker ()

@property (nonatomic,strong) UIView * contentView;

@property (nonatomic,strong) UIDatePicker * datePicker;

@property (nonatomic,strong) UIDatePicker * endDatePicker;

@property (nonatomic,strong) UIView * toolView;

@property (nonatomic,strong) UIButton * startBT;

@property (nonatomic,strong) UIButton * endBT;

@property (nonatomic,strong) UIButton * cuBT;

@property (nonatomic,strong) UIView * bottomToolView;

@property (nonatomic,strong) UIButton * cancelBT;

@property (nonatomic,strong) UIButton * makeSureBT;

@property (nonatomic,strong) NSDate * startDate;

@property (nonatomic,strong) NSDate * endDate;

@end

@implementation ECOrderDateSegePicker

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    WS(weakSelf, self);
    WS(weakWindow, ECAPPDelegate.effectWindow);
    self.contentView = [UIView new];
    self.contentView.backgroundColor = ECCOLOR(0xffffff);
    [ECAPPDelegate.effectWindow removeAllSubviews];
    [ECAPPDelegate.effectWindow addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(weakWindow);
        if (@available(iOS 11.0,*)) {
            make.top.mas_equalTo(weakWindow.mas_safeAreaLayoutGuideBottom).mas_offset(-300* PX_RELATIVE375);
        }else {
            make.top.mas_equalTo(weakWindow.mas_bottom).mas_offset(-300* PX_RELATIVE375);
        }
    }];
    
    self.toolView = [[UIView alloc] init];
    self.toolView.backgroundColor = ECCOLOR(0xffffff);
    [self.contentView addSubview:self.toolView];
    [self.toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(40*PX_RELATIVE375);
    }];
    
    self.startBT = [UIButton buttonWithType:UIButtonTypeCustom];
    self.startBT.titleLabel.numberOfLines = 0;
    self.startBT.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.startBT setTitleColor:ECCOLOR(0x333333) forState:UIControlStateSelected];
    [self.startBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
    [self.startBT setTitle:@"开始时间" forState:UIControlStateNormal];
    [self.startBT addTarget:self action:@selector(doSelected:) forControlEvents:UIControlEventTouchUpInside];
    self.startBT.titleLabel.font = ECFONT(14 * PX_RELATIVE375);
    [self.toolView addSubview:self.startBT];
    [self.startBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.mas_equalTo(weakSelf.toolView);
    }];
    
    self.endBT = [UIButton buttonWithType:UIButtonTypeCustom];
    self.endBT.titleLabel.numberOfLines = 0;
    self.endBT.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.endBT setTitleColor:ECCOLOR(0x333333) forState:UIControlStateSelected];
    [self.endBT setTitleColor:ECCOLOR(0x999999) forState:UIControlStateNormal];
    [self.endBT setTitle:@"结束时间" forState:UIControlStateNormal];
    [self.endBT addTarget:self action:@selector(doSelected:) forControlEvents:UIControlEventTouchUpInside];
    self.endBT.titleLabel.font = ECFONT(14 * PX_RELATIVE375);
    [self.toolView addSubview:self.endBT];
    [self.endBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.mas_equalTo(weakSelf.toolView);
        make.left.mas_equalTo(weakSelf.startBT.mas_right);
        make.width.mas_equalTo(weakSelf.startBT);
    }];
    
    self.bottomToolView = [UIView new];
    self.bottomToolView.backgroundColor = ECCOLOR(0xffffff);
    [self.contentView addSubview:self.bottomToolView];
    [self.bottomToolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.contentView);
        make.height.mas_equalTo(40 * PX_RELATIVE375);
        if (@available(iOS 11.0,*)) {
            make.bottom.mas_equalTo(weakSelf.contentView.mas_safeAreaLayoutGuideBottom);
        }else {
            make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom);
        }
    }];
    
    self.cancelBT = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancelBT setTitleColor:ECCOLOR_NAVIBACK forState:UIControlStateNormal];
    [self.cancelBT setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBT.titleLabel setFont:ECFONT(14 * PX_RELATIVE375)];
    [self.bottomToolView addSubview:self.cancelBT];
    [self.cancelBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(weakSelf.bottomToolView);
    }];
    [self.makeSureBT addTarget:self action:@selector(doCancel:) forControlEvents:UIControlEventTouchUpInside];
    
    self.makeSureBT = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.makeSureBT setTitleColor:ECCOLOR(0xffffff) forState:UIControlStateNormal];
    [self.makeSureBT setBackgroundImage:[UIImage imageWithColor:ECCOLOR_NAVIBACK] forState:UIControlStateNormal];
    [self.makeSureBT setTitle:@"确定" forState:UIControlStateNormal];
    [self.makeSureBT.titleLabel setFont:ECFONT(14 * PX_RELATIVE375)];
    [self.bottomToolView addSubview:self.makeSureBT];
    [self.makeSureBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(weakSelf.bottomToolView);
        make.left.mas_equalTo(weakSelf.cancelBT.mas_right);
        make.width.mas_equalTo(weakSelf.cancelBT);
    }];
    [self.makeSureBT addTarget:self action:@selector(useMakeSure:) forControlEvents:UIControlEventTouchUpInside];
    
    self.datePicker = [[UIDatePicker alloc] init];
    [self.datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hans"]];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.date = [NSDate date];
    [self.contentView addSubview:self.datePicker];
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(weakSelf.toolView.mas_bottom);
        make.bottom.mas_equalTo(weakSelf.bottomToolView.mas_top);
    }];
    [self.datePicker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    
    self.endDatePicker = [[UIDatePicker alloc] init];
    [self.endDatePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hans"]];
    self.endDatePicker.datePickerMode = UIDatePickerModeDate;
    self.endDatePicker.date = [NSDate date];
    [self.contentView addSubview:self.endDatePicker];
    [self.endDatePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(weakSelf.toolView.mas_bottom);
        make.bottom.mas_equalTo(weakSelf.bottomToolView.mas_top);
    }];
    [self.endDatePicker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    
    [self doSelected:self.startBT];
}

- (void)setStartDate:(NSDate *)startDate {
    _startDate = startDate;
    if (!_startDate) {
        [self.startBT setTitle:@"开始时间" forState:UIControlStateNormal];
    }
    NSDateFormatter * fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyy-MM-dd"];
    NSString * dateStr = [fmt stringFromDate:_startDate];
    [self.startBT setTitle:[NSString stringWithFormat:@"开始时间\n%@",dateStr] forState:UIControlStateNormal];
}

- (void)setEndDate:(NSDate *)endDate {
    _endDate = endDate;
    if (!_endDate) {
        [self.endBT setTitle:@"结束时间" forState:UIControlStateNormal];
    }
    NSDateFormatter * fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyy-MM-dd"];
    NSString * dateStr = [fmt stringFromDate:_endDate];
    [self.endBT setTitle:[NSString stringWithFormat:@"结束时间\n%@",dateStr] forState:UIControlStateNormal];
}

- (void)dateChange:(UIDatePicker*)datePicker {
    if (datePicker == self.datePicker) {
        self.startDate = [datePicker date];
    }else {
        self.endDate = [datePicker date];
    }
}

- (void)show {
    WS(weakSelf, self);
    [ECAPPDelegate showEffectWindow:^(UIWindow * _Nonnull effectWindow) {
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.contentView.alpha = 1;
        }];
    }];
}

- (void)doSelected:(UIButton*)sender {
    if (self.cuBT) {
        self.cuBT.selected = !self.cuBT.selected;
    }
    self.cuBT = sender;
    self.cuBT.selected = !self.cuBT.selected;
    if (self.cuBT == self.startBT) {
        self.datePicker.hidden = NO;
        self.endDatePicker.hidden = YES;
    }else {
        self.datePicker.hidden = YES;
        self.endDatePicker.hidden = NO;
    }
}

- (void)useMakeSure:(UIButton*)sender {
    if (self.makeSureBlock) {
        self.makeSureBlock(self.startDate, self.endDate);
    }
    [self hideWindow];
}

- (void)doCancel:(UIButton*)sender {
    [self hideWindow];
    self.startDate = nil;
    self.endDate = nil;
    self.datePicker.date = [NSDate date];
    self.endDatePicker.date = [NSDate date];
}

- (void)hideWindow {
    WS(weakSelf, self);
    [ECAPPDelegate hiddenEffectWindow:^(UIWindow * _Nonnull effectWindow) {
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.contentView.alpha = 0;
        }];
    }];
}

@end
